package com.icss.service.impl;

import com.icss.dao.RecordDao;
import com.icss.dao.UserDao;
import com.icss.entity.Record;
import com.icss.entity.User;
import com.icss.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;

import javax.persistence.Query;
import javax.persistence.criteria.*;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@Service
@Component
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordDao recordDao;
    @Autowired
    private UserDao userDao;

    /**
     * 分页查询所有
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<Record> selectRecordAll(int pageNum, int pageSize) {
        Sort sort = Sort.by(Sort.Direction.ASC, "state");
        PageRequest page = PageRequest.of(pageNum, pageSize, sort);
        Page<Record> records = recordDao.findAll(page);
        return records;
    }

    /**
     * 根据条件进行查询
     * @param pageNum1
     * @param pageSize1
     * @param carNumber
     * @param illegalStartTime
     * @param illegalEndTime
     * @return
     */
    @Override
    public Page<Record> conditionQuery(int pageNum1, int pageSize1,String carNumber, String illegalStartTime, String illegalEndTime) {
        Specification<Record> specification = new Specification<Record>() {
            @Override
            public Predicate toPredicate(Root<Record> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicatesList = new ArrayList<Predicate>();
                Predicate like = criteriaBuilder.like(root.get("carNumber"), "%" + carNumber + "%");
                System.out.println(like.toString());
                predicatesList.add(like);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat sdfmat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    predicatesList.add(criteriaBuilder.between(root.get("illegalTime"),
                            sdfmat.parse(sdfmat.format(sdf.parse(illegalStartTime).getTime())),
                            sdfmat.parse(sdfmat.format(sdf.parse(illegalEndTime).getTime() + 86400000))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Predicate[] array = new Predicate[predicatesList.size()];
                System.out.println(array.toString());
                Predicate and = criteriaBuilder.and(predicatesList.toArray(array));
                System.out.println(and.toString());
                return and;
            }
        };
        //如果都为空时点击查询按钮
        if (carNumber==null&&illegalEndTime==null&&illegalEndTime==null){
            Page<Record> recordspage = selectRecordAll(pageNum1, pageSize1);
            return recordspage;
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "state");
        PageRequest page1 = PageRequest.of(pageNum1, pageSize1, sort);
        Page<Record> records = recordDao.findAll(specification,page1);
        return records;
    }

    @Override
    public List<Record> showRecord() {
        List<Record> add = recordDao.findAll();
        return add;
    }

    @Override
    public void addRecord(Record record) {
        recordDao.save(record);
    }

    @Override
    public void editRecord(Record record) {
            recordDao.save(record);
    }

    /**
     *
     * @param balance
     * @param ablescore
     * @param userId
     */
    @Override
    @Transactional
    public Integer manageRecord(Integer balance,Integer ablescore,Integer userId) {
        recordDao.updateByuserName(balance,ablescore,userId);
        recordDao.updateState(userId);
        return 1;
    }
}
